<template>
  <div class="add-lease-drawer">
    <el-drawer
      :title="
        assessInfo && assessInfo.assessmentId
          ? '编辑公寓评估信息'
          : '添加公寓评估信息'
      "
      :visible.sync="drawer"
      direction="rtl"
      :wrapperClosable="false"
      :modal-append-to-body="false"
      :show-close="false"
      size="30%"
    >
      <div class="add-property-content">
        <el-form ref="form" :model="form" label-width="150px" size="small">
          <div class="add-property-content-scroll">
            <el-row>
              <el-col :span="18">
                <el-form-item label="评估报告日期：">
                  <el-date-picker
                    v-model="form.assessReportDate"
                    type="date"
                    placeholder="选择日期"
                    value-format="yyyy-MM-dd"
                    style="width: 100%"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="评估价值时点">
                  <el-date-picker
                    v-model="form.assessValueDate"
                    type="date"
                    placeholder="选择日期"
                    value-format="yyyy-MM-dd"
                    style="width: 100%"
                  >
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="22">
                <el-form-item
                  label="评估价值金额（元/天/㎡）："
                  label-width="100%"
                >
                  <el-table
                    :data="tableData"
                    border
                    style="margin-bottom: 10px"
                  >
                    <el-table-column label="套型">
                      <template slot-scope="scope">
                        <span> {{ scope.row.jacketed }}</span>
                      </template>
                    </el-table-column>
                    <el-table-column label="1 人间">
                      <template slot-scope="scope">
                        <el-input
                          v-model="scope.row.onePerson"
                          size="mini"
                        ></el-input>
                      </template>
                    </el-table-column>
                    <el-table-column label="2 人间">
                      <template slot-scope="scope">
                        <el-input
                          v-model="scope.row.twoPerson"
                          size="mini"
                        ></el-input>
                      </template>
                    </el-table-column>
                    <el-table-column label="4 人间">
                      <template slot-scope="scope">
                        <el-input
                          v-model="scope.row.fourPerson"
                          size="mini"
                        ></el-input>
                      </template>
                    </el-table-column>
                    <el-table-column label="6 人间">
                      <template slot-scope="scope">
                        <el-input
                          v-model="scope.row.sixerson"
                          size="mini"
                        ></el-input>
                      </template>
                    </el-table-column>
                    <el-table-column label="8 人间">
                      <template slot-scope="scope">
                        <el-input
                          v-model="scope.row.eightPerson"
                          size="mini"
                        ></el-input>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="18">
                <el-form-item label="评估报告：" label-width="100%">
                  <el-upload
                    class="upload-demo"
                    action="/admin/common/upload"
                    :headers="headersAuthorization"
                    :on-success="
                      (response, file, fileList) =>
                        handlePgSuccess(response, file, fileList)
                    "
                    :on-remove="
                      (file, fileList) => handleRemove(file, fileList, 'pg')
                    "
                    multiple
                    :limit="3"
                    :file-list="assessReportList"
                  >
                    <el-button size="small" type="primary">点击上传</el-button>
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>

            <el-row>
              <el-col :span="18">
                <el-form-item label="其他附件：" label-width="100%">
                  <el-upload
                    class="upload-demo"
                    action="/admin/common/upload"
                    :headers="headersAuthorization"
                    :on-success="
                      (response, file, fileList) =>
                        handleQtSuccess(response, file, fileList)
                    "
                    :on-remove="
                      (file, fileList) => handleRemove(file, fileList, 'qt')
                    "
                    multiple
                    :limit="3"
                    :file-list="otherList"
                  >
                    <el-button size="small" type="primary">点击上传</el-button>
                  </el-upload>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
          <el-form-item class="opt-cla">
            <el-button type="primary" @click="onSubmit">
              <template v-if="assessInfo && assessInfo.assessmentId">
                编辑
              </template>
              <template v-else>添加</template></el-button
            >
            <el-button @click="cancel">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { addAssessment, editAssessment } from "@/api/shops";
import { getToken } from "@/utils/auth";
export default {
  props: {
    drawer: {
      type: Boolean,
      default: false,
    },
    assessInfo: {},
  },
  data() {
    return {
      form: {
        assessReportAttachIds: undefined,
        otherAttachIds: undefined,
        assessValueDate: undefined,
        assessReportDate: undefined,
        propertyUnitId: undefined,
        assessmenSuiteList: [],
      },
      otherList: [], // 其他附件
      assessReportList: [], //评估报告
      headersAuthorization: null,
      tableData: [
        {
          jacketed: "数量",
          onePerson: 0,
          twoPerson: 0,
          fourPerson: 0,
          sixerson: 0,
          eightPerson: 0,
        },
        {
          jacketed: "评估金额（元/天/套）",
          onePerson: "",
          twoPerson: "",
          fourPerson: "",
          sixerson: "",
          eightPerson: "",
        },
      ],

      assessmenSuiteList: [
        {
          sort: 1,
          suiteType: "1人间",
          usedNum: 0,
          assessAmount: 0,
        },
        {
          sort: 2,
          suiteType: "2人间",
          usedNum: 0,
          assessAmount: 0,
        },
        {
          sort: 3,
          suiteType: "4人间",
          usedNum: 0,
          assessAmount: 0,
        },
        {
          sort: 4,
          suiteType: "6人间",
          usedNum: 0,
          assessAmount: 0,
        },
        {
          sort: 5,
          suiteType: "8人间",
          usedNum: 0,
          assessAmount: 0,
        },
      ],
    };
  },
  watch: {
    assessInfo: {
      handler(val) {
        // 编辑
        if (val && val.assessmentId) {
          this.form = Object.assign({}, val);
          this.form.assessmenSuiteList.forEach((v) => {
            if (v.suiteType == "1人间") {
              this.tableData[0].onePerson = v.usedNum;
              this.tableData[1].onePerson = v.assessAmount;
            } else if (v.suiteType == "2人间") {
              this.tableData[0].twoPerson = v.usedNum;
              this.tableData[1].twoPerson = v.assessAmount;
            } else if (v.suiteType == "4人间") {
              this.tableData[0].fourPerson = v.usedNum;
              this.tableData[1].fourPerson = v.assessAmount;
            } else if (v.suiteType == "6人间") {
              this.tableData[0].sixerson = v.usedNum;
              this.tableData[1].sixerson = v.assessAmount;
            } else if (v.suiteType == "8人间") {
              this.tableData[0].eightPerson = v.usedNum;
              this.tableData[1].eightPerson = v.assessAmount;
            }
          });

          if (
            this.form.assessReportAttachList &&
            this.form.assessReportAttachList.length > 0
          ) {
            this.assessReportList = this.form.assessReportAttachList.map(
              (v) => {
                return {
                  name: v.originalAttName,
                  url: v.attUrl,
                };
              }
            );
          }

          if (
            this.form.otherAttachList &&
            this.form.otherAttachList.length > 0
          ) {
            this.otherList = this.form.otherAttachList.map((v) => {
              return {
                name: v.originalAttName,
                url: v.attUrl,
              };
            });
          }
        }
      },
      immediate: true,
      deep: true,
    },
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      // 获取用于上传的token
      this.headersAuthorization = {
        Authorization: "Bearer " + getToken(),
      };
    },

    handleRemove(file, fileList, type) {
      switch (type) {
        // 其他附件
        case "qt":
          let bdcTarget = fileList.map((item) => item.response.data.attId);
          if (bdcTarget.length > 0) {
            this.form.immovablePropertyCertificateAttachIds =
              bdcTarget.join(",");
          }else{
            this.form.immovablePropertyCertificateAttachIds =''
          }
          break;
        // 评估报告
        case "pg":
          let fczTarget = fileList.map((item) => item.response.data.attId);
          if (fczTarget.length > 0) {
            this.form.houseOwnershipCertificateAttachIds = fczTarget.join(",");
          }else{
            this.form.houseOwnershipCertificateAttachIds =''
          }
          break;
      }
    },
    // 其他附件
    handleQtSuccess(res, file, fileList) {
      if (res.code != 200) {
        this.$message.error("上传失败");
        return;
      }

      let target = fileList.map((item) => item.response.data.attId);
      if (target.length > 0) {
        this.form.otherAttachIds = target.join(",");
      }
    },
    handlePgSuccess(res, file, fileList) {
      if (res.code != 200) {
        this.$message.error("上传失败");
        return;
      }

      let target = fileList.map((item) => item.response.data.attId);
      if (target.length > 0) {
        this.form.assessReportAttachIds = target.join(",");
      }
    },
    clear() {
      this.otherList = [];
      this.assessReportList = [];
      this.form = {
        assessReportAttachIds: undefined,
        otherAttachIds: undefined,
        assessValueDate: undefined,
        assessReportDate: undefined,
        propertyUnitId: undefined,
        assessmenSuiteList: [],
      };
    },
    cancel() {
      this.$emit("update:drawer", false);
      this.clear();
    },
    onSubmit() {
      this.assessmenSuiteList.forEach((v) => {
        if (v.suiteType == "1人间") {
          v.usedNum = this.tableData[0].onePerson;
          v.assessAmount = this.tableData[1].onePerson;
        } else if (v.suiteType == "2人间") {
          v.usedNum = this.tableData[0].twoPerson;
          v.assessAmount = this.tableData[1].twoPerson;
        } else if (v.suiteType == "4人间") {
          v.usedNum = this.tableData[0].fourPerson;
          v.assessAmount = this.tableData[1].fourPerson;
        } else if (v.suiteType == "6人间") {
          v.usedNum = this.tableData[0].sixerson;
          v.assessAmount = this.tableData[1].sixerson;
        } else if (v.suiteType == "8人间") {
          v.usedNum = this.tableData[0].eightPerson;
          v.assessAmount = this.tableData[1].eightPerson;
        }
      });
      if (this.form && this.form.assessmentId) {
        this.form.assessmenSuiteList = this.assessmenSuiteList;
        let params = {
          ...this.form,
        };
        editAssessment(params)
          .then((res) => {
            if (res.code == 200) {
              this.$message.success("编辑成功");
              this.$emit("update:drawer", false);
              this.$emit("childGyUnitEvent");
              this.clear();
            }
          })
          .catch((e) => {
            this.$message.error(e.msg);
          });
      } else {
        this.form.assessmenSuiteList = this.assessmenSuiteList;
        this.form.propertyUnitId = this.$route.query.propertyUnitId;
        let params = {
          ...this.form,
        };
        addAssessment(params)
          .then((res) => {
            if (res.code == 200) {
              this.$message.success("新增成功");
              this.$emit("update:drawer", false);
              this.$emit("childGyUnitEvent");
              this.clear();
            }
          })
          .catch((e) => {
            this.$message.error(e.msg);
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";

.add-lease-drawer {
  .add-property-content {
    width: 100%;
    .add-property-content-scroll {
      height: calc(100vh - 112px);
      overflow-y: auto;
    }
  }
  .upload-img-opt {
    display: flex;
    flex-direction: column;
    // margin-top: -25px;
    .el-upload__text {
      font-size: 14px;
      font-style: normal;
      line-height: 0;
      padding-top: 10px;
    }
    .img-content {
      flex: 1;
      background: #fafafa;
      margin-left: 6px;
    }
  }
  .upload-quanz {
    width: 90%;
    height: 120px;
    background: #fafafa;
    font-size: 14px;
    .fcz {
      display: flex;
      align-items: center;
      margin: 15px 0 0 15px;
    }
    .tdz {
      display: flex;
      align-items: center;
      margin: 0 0 0 15px;
    }
  }
  .type-content {
    width: 98%;
    background-color: #fafafa;
    padding: 0 20px 20px;
    height: auto;
    // overflow: auto;
    .add-type {
      width: 84.5%;
      height: 30px;
      line-height: 30px;
      text-align: center;
      color: #dcdcdc;
      border: 1px dotted #dcdcdc;
      background: #fff;
      cursor: pointer;
    }
    tr {
      width: 100%;
      display: flex;
      align-items: center;
      margin-bottom: 10px;
      th {
        text-align: center;
        font-size: 14px;
        color: #505a69;
        padding: 10px 0;
        &:nth-child(1) {
          width: 35%;
        }
        &:nth-child(2) {
          width: 50%;
        }
      }
      td {
        &:nth-child(1) {
          width: 35%;
        }
        &:nth-child(2) {
          width: 55%;
        }
        &:nth-child(3) {
          width: 10%;
          img {
            width: 24px;
            height: 24px;
            cursor: pointer;
          }
        }
      }
    }
  }
}
</style>
<style lang="scss">
.add-lease-drawer {
  .el-drawer__header {
    background-color: #fafafa;
    padding: 0;
    color: #000;
    margin-bottom: 5px;
  }
  .quanType {
    .el-form-item__label {
      width: 90px !important;
    }
  }
  .el-form-item__label {
    color: #000;
  }
  header#el-drawer__title {
    padding: 10px;
  }
  .el-form-item__content {
    display: contents;
  }
  .el-col {
    margin-left: 20px;
  }
  .el-form-item__label {
    text-align: left;
  }
  .el-form-item--small.el-form-item {
    margin-bottom: 10px;
  }
  .el-upload--picture-card {
    background-color: #fafafa;
    display: flex;
    flex-direction: column;
    padding-top: 40px;
    i {
      font-size: 35px;
    }
  }
  .opt-cla {
    margin: 20px;
    .el-button--default,
    .el-button--primary {
      width: 95px;
      height: 32px;
      padding: 0;
    }
  }
}
</style>